package com.qpcy.service.impl;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.qpcy.dto.CasesDto;
import com.qpcy.entity.Cases;
import com.qpcy.entity.ProductCase;
import com.qpcy.mapper.CasesMapper;
import com.qpcy.mapper.ProductCaseMapper;
import com.qpcy.service.CasesService;
import com.qpcy.utils.ResponseResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import static com.qpcy.entity.table.CasesTableDef.CASES;
import static com.qpcy.entity.table.ProductCaseTableDef.PRODUCT_CASE;

/**
 * 案例 服务层实现。
 *
 * @author DELL
 * @since 2024-10-27
 */
@Service
@Transactional
public class CasesServiceImpl extends ServiceImpl<CasesMapper, Cases> implements CasesService {

    @Autowired
    private CasesMapper casesMapper;

    @Autowired
    private ProductCaseMapper productCaseMapper;

    @Override
    public ResponseResult getDetail(int casesId) {
        return ResponseResult.success(casesMapper.selectOneWithRelationsById(casesId));
    }

    @Override
    public ResponseResult saveCases(CasesDto dto) {
        Cases cases = new Cases();
        BeanUtils.copyProperties(dto, cases, "caseId");
        casesMapper.insert(cases);

        List<ProductCase> productCases = dto.getProductIds().stream()
                .map(item -> {
                    ProductCase productCase = new ProductCase();
                    productCase.setCaseId(cases.getCaseId());
                    productCase.setProductId(item);
                    return productCase;
                }).collect(Collectors.toList());

        productCaseMapper.insertBatch(productCases);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult getAll(int page, int size) {
        Page<Cases> casesPage = new Page<>(page, size);
        QueryWrapper queryWrapper = QueryWrapper.create();
        Page<Cases> result = casesMapper.paginateWithRelations(casesPage, queryWrapper);
        return ResponseResult.success(result);
    }

    @Override
    public ResponseResult updateCases(CasesDto dto) {
        Cases cases = new Cases();
        BeanUtils.copyProperties(dto, cases);
        casesMapper.update(cases);

        QueryWrapper queryWrapper = QueryWrapper.create()
                .where(PRODUCT_CASE.CASE_ID.eq(dto.getCaseId()));
        productCaseMapper.deleteByQuery(queryWrapper);

        List<ProductCase> productCases = dto.getProductIds().stream()
                .map(item -> {
                    ProductCase productCase = new ProductCase();
                    productCase.setCaseId(dto.getCaseId());
                    productCase.setProductId(item);
                    return productCase;
                }).collect(Collectors.toList());

        productCaseMapper.insertBatch(productCases);
        return ResponseResult.success();
    }

}
